<template>
  <div class="infoMent">

    <el-form :inline="true" class="demo-form-inline" size="small">
      <el-form-item>
        <el-button type="primary" @click="addStudent">新增</el-button>
      </el-form-item>
    </el-form>

    <el-dialog :title="isAdd ? '添加学生信息' : '修改学生信息'" :visible.sync="dialogFormVisible" width="500px">
      <el-form :model="form" status-icon :rules="rules" ref="infoForm">
        <el-form-item label="姓名" :label-width="formLabelWith" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWith" prop="sex">
          <el-radio v-model="form.sex" label="1">男</el-radio>
          <el-radio v-model="form.sex" label="2">女</el-radio>
        </el-form-item>
        <el-form-item label="年龄" :label-width="formLabelWith" prop="age">
          <el-input v-model.number="form.age" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="父亲" :label-width="formLabelWith" prop="father">
          <el-input v-model="form.father" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="母亲" :label-width="formLabelWith" prop="mather">
          <el-input v-model="form.mather" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="家庭地址" :label-width="formLabelWith" prop="address">
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="入校时间" :label-width="formLabelWith" prop="time">
          <el-date-picker
              v-model="form.time"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="联系方式" :label-width="formLabelWith" prop="phone">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelEdit('infoForm')">取 消</el-button>
        <el-button type="primary" @click="sure('infoForm')">确 定</el-button>
      </div>
    </el-dialog>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="name" label="姓名" align="center"></el-table-column>
      <el-table-column prop="sex" label="性别" align="center"></el-table-column>
      <el-table-column prop="age" label="年龄" align="center"></el-table-column>
      <el-table-column prop="father" label="父亲" align="center"></el-table-column>
      <el-table-column prop="mather" label="母亲" align="center"></el-table-column>
      <el-table-column prop="address" label="家庭地址" align="center"></el-table-column>
      <el-table-column prop="time" label="入校时间" align="center"></el-table-column>
      <el-table-column prop="phone" label="联系方式" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
              type="danger"
              size="mini"
              icon="el-icon-edit"
              @click="edit(scope.row)">
          </el-button>
          <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="del(scope.row)">
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>

import {delInfo, getInfo, info, updateInfo} from "@/router/svc/stu";
import {Message} from "element-ui";

export default {
  data() {
    return {
      tableData: [],
      formInline: {},
      dialogFormVisible: false,
      formLabelWith: '80px',
      isAdd: true,
      form: {
        name: '',
        age: '',
        sex: '',
        father: '',
        mather: '',
        address: '',
        time: '',
        phone: ''
      },
      rules: {
        name: [
          {required: true, message: '请输入用户名', trigger: 'blur'}
        ],
        sex: [{required: true, message: '请选择性别'}],
        age: [
          {required: true, message: '年龄不能为空'},
          {type: 'number', message: '年龄必须为数字值'}
        ],
        address: [{required: true, message: '请输入地址', trigger: 'blur'}],
        time: [{required: true, message: '请输入入学时间', trigger: 'blur'}],
        phone: [{required: true, message: '请输入联系方式', trigger: 'blur'}]
      }
    };
  },
  methods: {
    del(row) {
      this.$messageBox.alert('确认删除数据', '提示', {
        confirmButtonText: '确定',
        callback: () => {
          delInfo(row.name).then(res => {
            if (res.data.code === 200) {
              Message({type: "success", message: '成功删除学生信息: ' + res.data.data});
            }
            this.getData()
          })
        }
      })
    },
    edit(row) {
      this.dialogFormVisible = true
      this.form = {...row}
      this.isAdd = false

    },
    cancelEdit(formName) {
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    addStudent() {
      this.dialogFormVisible = true
      this.form = {
        name: '',
        age: '',
        sex: '',
        father: '',
        mather: '',
        address: '',
        time: '',
        phone: ''
      }
      this.isAdd = false
    },
    getData() {
      getInfo().then(res => {
        if (res.data.code === 200) {
          this.tableData = res.data.data
        }
      })
    },
    sure(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.isAdd) {
            info(this.form).then(res => {
              if (res.data.code === 200) {
                Message({type: 'success', message: '新增学生: ' + res.data.data + ' 成功'})
                this.dialogFormVisible = false
                this.$refs[formName].resetFields()
              }
            })
          } else {
            updateInfo(this.form).then(res => {
              if (res.data.code === 200) {
                Message({type: 'success', message: '更新学生: ' + res.data.data + ' 成功'})
                this.dialogFormVisible = false
                this.$refs[formName].resetFields()
              }
            })
          }

        }
      })
      this.getData()
    }

  },
  created() {
    this.getData()
  }
}
</script>
<style lang="scss">
.infoMent {
  .demo-form-inline, .el-form-item {
    text-align: left;
  }

  .el-pagination {
    text-align: left;
    margin-top: 20px;
  }
}
</style>>